package sumoTween.demo.swing.keyview;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;

import sumoTween.KeyFloat;
import sumoTween.Tweener;
import sumoTween.easing.Back;
import sumoTween.easing.Bounce;
import sumoTween.easing.Circ;
import sumoTween.easing.Cubic;
import sumoTween.easing.EasingIF;
import sumoTween.easing.Expo;
import sumoTween.easing.Linear;
import sumoTween.easing.Quad;
import sumoTween.easing.Quart;
import sumoTween.easing.Quint;
import sumoTween.easing.Sine;
import sumoTween.events.KeyEvent;

public class DemoComponent extends JComponent 
{
	Tweener testTween=new Tweener();
	Font font;
	int currentEasing=0;

	int mousex=0;
	int mousey=0;

	float scale=100f;

	Color bgcol=new Color(0xeeeeee);

	EasingIF easings[]={new Linear(),new Sine(),new Quad(),new Cubic(),new Expo(),new Back(),new Bounce(),new Quart(),new Quint(),new Circ()};
	
	public DemoComponent()
	{
		font=new Font("Arial", Font.PLAIN,10);
		init();

		this.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent arg0) 
			{
				for(int i=0;i<=testTween.getLastKey().getTime();i++)
				{
					if(mousex<(i+1)*scale && mousex>(i*scale))
					{
						makeRandomEasing(i);
					}
				}
			}
		});
		
		
		this.addMouseMotionListener(new MouseMotionListener() {
			
			public void mouseMoved(MouseEvent e) 
			{
				mousex=e.getX();
				mousey=e.getY();
			}
			
			public void mouseDragged(MouseEvent e) {}
		});
	}

	void makeRandomEasing(float time)
	{
		KeyFloat kf=testTween.getKey(time);
		
		kf.setEasingDirection((int)(Math.random()*4)-1);
		kf.setEasing(easings[(int)(Math.random()*easings.length)]);
	}

	void init()
	{
		
		
		testTween.clear();
		testTween.set(0f, 2,Tweener.EASING_OUT, new Cubic());
		testTween.set(1f, 200f,Tweener.EASING_INOUT,new Cubic()); 
		testTween.set(2f, 100f,Tweener.EASING_INOUT,new Back());
		testTween.set(3f, 300f,Tweener.EASING_INOUT,new Cubic());
		testTween.set(4f, 0f,Tweener.EASING_OUT,new Bounce());
		testTween.set(5f, 130f,Tweener.EASING_INOUT,new Linear());
		testTween.set(6f, 190f,Tweener.EASING_INOUT,new Linear());
		testTween.set(7f, 100f,Tweener.EASING_INOUT,new Linear(),new KeyEvent() 
		{
			public void KeyEvent(KeyFloat keyFloat) 
			{
				System.out.println("loop.....");
				//init();
				testTween.resetTimer();
			}
		});
	}
	
	public void paint(Graphics g)
	{
		g.setFont(font);
		g.setColor(new Color(0xffffff));
		g.fillRect(0, 0, getWidth(), getHeight());


		float incr=0.02f;
		
		Graphics2D g2d=(Graphics2D)g;
	    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
	    
		for(int i=0;i<=testTween.getLastKey().getTime();i++)
		{
			if(mousex<(i+1)*scale && mousex>(i*scale))
			{
				g.setColor(bgcol);
				g.fillRect((int)(i*scale), 0, 100, 500);
			}
			
			g.setColor(Color.gray);
			g.drawString(testTween.getKey((float)i+0.1f).toString(), (int)(i*scale)+10, 20);

			g.drawLine((int)(i*scale), 0, (int)(i*scale), 500);
		}

		
		
		
		g.setColor(new Color(0x555555));
		
		g.fillRect((int)(testTween.getTime()*100f)-5,testTween.getIntValue()-5, 10, 10);
		
		
		for(float f=0.0f;f<=testTween.getLastKey().getTime()+1f;f+=incr)
		{
			int x=(int)(f*scale);
			int x2=(int)((f+incr)*scale);
			
			g.setColor(Color.black);
			g.drawLine(x, testTween.getIntValue(f,false), x2, testTween.getIntValue(f+incr,false));
		}


	}


}






